function trim(str, chars) {
	if(chars!=null){
		return ltrim(rtrim(str, chars), chars);
	}else{
		return ltrim(rtrim(str, " "), " ");
	}
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}